package com.weiwei.base.fragment;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hwtx.jytxdh.R;


/**
 * 
 * @instruction 主界面导航栏
 * @author Sundy 兴
 * @version 创建时间：2014-12-16上午11:44:02
 */
public class FragmentIndicator extends LinearLayout implements OnClickListener {

	private OnIndicateListener mOnIndicateListener;
	// 拨号图标动画
	private Animation animDown = null;
	private Animation animUp = null;

	/**
	 * 容器
	 */
	private Context mContext;

	/**
	 * 拨号
	 */
	private LinearLayout vs_keyborad_layout;
	// 拨号图标
	private ImageView vs_keyborad_imagview;
	// 拨号文字
	private TextView vs_keyborad_tv;
	static TextView vs_mms_unread;

	/**
	 * 联系人
	 */
	private LinearLayout vs_contact_layout;
	// 联系人图标
	private ImageView vs_contact_imagview;
	// 联系人文字
	private TextView vs_contact_tv;

	/**
	 * 话费
	 */
	private LinearLayout vs_money_layout;
	// 话费图标
	private ImageView vs_money_imagview;
	// 话费文字
	private TextView vs_money_tv;

//	/**
//	 * 信息
//	 */
//	private LinearLayout vs_mms_layout;
//	// 信息图标
//	private ImageView vs_mms_imagview;
//	// 信息文字
//	private TextView vs_mms_tv;
	/**
	 * 发现
	 */
	private LinearLayout vs_found_layout;
	private ImageView vs_found_imagview;
	private TextView vs_found_tv;

	/**
	 * 我
	 */
	private LinearLayout vs_me_layout;
	// 我的图标
	private ImageView vs_me_imagview;
	// 我的图标文字
	private TextView vs_me_tv;

	private static ImageView vs_mms_imagview_red;

	/*
	 * //服务 private LinearLayout vs_service_layout; // 服务图标 private ImageView
	 * vs_service_imagview; // 服务文字 private TextView vs_service_tv;
	 */
	private FragmentIndicator(Context context) {
		super(context);
		this.mContext = context;
		initView(context);
	}

	public FragmentIndicator(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
		initView(context);
	}

	/**
	 * 初始化视图
	 */
	private void initView(Context context) {
		// 导入布局
		LayoutInflater.from(context).inflate(R.layout.framement_tab_layout, this, true);

		vs_keyborad_layout = (LinearLayout) findViewById(R.id.vs_keyborad_layout);
		vs_keyborad_imagview = (ImageView) findViewById(R.id.vs_keyborad_imagview);
		vs_keyborad_tv = (TextView) findViewById(R.id.vs_keyborad_tv);

		vs_contact_layout = (LinearLayout) findViewById(R.id.vs_contact_layout);
		vs_contact_imagview = (ImageView) findViewById(R.id.vs_contact_imagview);
		vs_contact_tv = (TextView) findViewById(R.id.vs_contact_tv);
		vs_mms_unread = (TextView) findViewById(R.id.vs_mms_unread);
		// vs_money_layout = (LinearLayout) findViewById(R.id.vs_money_layout);
		// vs_money_imagview = (ImageView) findViewById(R.id.vs_money_imagview);
		// vs_money_tv = (TextView) findViewById(R.id.vs_money_tv);
		vs_found_layout = (LinearLayout) findViewById(R.id.vs_found_layout);
		vs_found_imagview = (ImageView) findViewById(R.id.vs_found_imagview);
		vs_found_tv = (TextView) findViewById(R.id.vs_found_tv);

//		vs_mms_layout = (LinearLayout) findViewById(R.id.vs_mms_layout);
//		vs_mms_imagview = (ImageView) findViewById(R.id.vs_mms_imagview);
//		vs_mms_tv = (TextView) findViewById(R.id.vs_mms_tv);

		vs_me_layout = (LinearLayout) findViewById(R.id.vs_me_layout);
		vs_me_imagview = (ImageView) findViewById(R.id.vs_me_imagview);
		vs_me_tv = (TextView) findViewById(R.id.vs_me_tv);
		vs_mms_imagview_red = (ImageView) findViewById(R.id.vs_contact_layout_red);

		/*
		 * vs_service_layout = (LinearLayout)
		 * findViewById(R.id.vs_service_layout); vs_service_imagview =
		 * (ImageView) findViewById(R.id.vs_service_imagview); vs_service_tv =
		 * (TextView) findViewById(R.id.vs_service_tv);
		 */

		// 设置监听事件
		vs_keyborad_layout.setOnClickListener(this);
		vs_contact_layout.setOnClickListener(this);
		// vs_money_layout.setOnClickListener(this);
//		vs_mms_layout.setOnClickListener(this);
		vs_me_layout.setOnClickListener(this);
		vs_found_layout.setOnClickListener(this);
		// vs_service_layout.setOnClickListener(this);

	}

	/**
	 * 设置键盘开关图片
	 */
	public void setTabImage(boolean isopen) {
		if (isopen) {
			vs_keyborad_imagview.setBackgroundResource(R.drawable.ic_dial_normal_down);
		} else {
			vs_keyborad_imagview.setBackgroundResource(R.drawable.ic_dial_normal);
		}
	}

	/**
	 * 设置键盘开关按钮你状态
	 */
	public void setTabImageFouse2(boolean isopen) {
		// 直接默认
		vs_keyborad_imagview.setBackgroundResource(R.drawable.ic_dial_focused);
		animDown = AnimationUtils.loadAnimation(mContext, R.anim.call_tab_logo_anim_down);
		LinearInterpolator lin = new LinearInterpolator();
		animDown.setInterpolator(lin);
		animDown.setFillAfter(true);
		animUp = AnimationUtils.loadAnimation(mContext, R.anim.call_tab_logo_anim_up);
		LinearInterpolator linUp = new LinearInterpolator();
		animUp.setInterpolator(linUp);
		animUp.setFillAfter(true);

		if (isopen) {
			vs_keyborad_imagview.startAnimation(animDown);
			vs_keyborad_tv.setText("最近通话");
		} else {
			vs_keyborad_imagview.startAnimation(animUp);
			vs_keyborad_tv.setText("拨号");
		}

	}

	/**
	 * 设置键盘开关按钮你状态
	 */
	public void setTabImageFouse(boolean isopen) {
		if (isopen) {
			vs_keyborad_imagview.setBackgroundResource(R.drawable.ic_dial_focused_down);
		} else {
			vs_keyborad_imagview.setBackgroundResource(R.drawable.ic_dial_focused);
		}
	}

	public void setTabText(boolean isopen) {
		if (isopen) {
			vs_keyborad_tv.setText("最近通话");
		} else {
			vs_keyborad_tv.setText("拨号");
		}
	}

	/**
	 * 设置按钮状态
	 * 
	 * @param which
	 */
	public void setIndicator(int which) {
		switch (which) {
		case 0:
			vs_contact_imagview.setBackgroundResource(R.drawable.ic_contacts_list_normal);
			// vs_money_imagview.setBackgroundResource(R.drawable.ic_recharge_normal);
			vs_found_imagview.setBackgroundResource(R.drawable.ic_service);
			vs_me_imagview.setBackgroundResource(R.drawable.ic_myself_normal);
//			vs_mms_imagview.setBackgroundResource(R.drawable.ic_mms_normal);

			vs_keyborad_tv.setTextColor(mContext.getResources().getColor(R.color.vs_blue_sdeep));
			vs_contact_tv.setTextColor(mContext.getResources().getColor(R.color.text_gray));
			// vs_money_tv.setTextColor(mContext.getResources().getColor(R.color.text_gray));
			vs_found_tv.setTextColor(mContext.getResources().getColor(R.color.text_gray));
//			vs_mms_tv.setTextColor(mContext.getResources().getColor(R.color.text_gray));
			vs_me_tv.setTextColor(mContext.getResources().getColor(R.color.text_gray));
			/*
			 * vs_service_tv.setTextColor(mContext.getResources().getColor(R.color
			 * .text_gray));
			 * vs_service_imagview.setBackgroundResource(R.drawable.ic_service);
			 */
			break;
//		case 1:
//			vs_contact_imagview.setBackgroundResource(R.drawable.ic_contacts_list_normal);
//			// vs_money_imagview.setBackgroundResource(R.drawable.ic_recharge_focused);
//			vs_found_imagview.setBackgroundResource(R.drawable.ic_service);
//			vs_me_imagview.setBackgroundResource(R.drawable.ic_myself_normal);
//			vs_mms_imagview.setBackgroundResource(R.drawable.ic_mms_focused);
//
//			vs_keyborad_tv.setTextColor(mContext.getResources().getColor(R.color.text_gray));
//			vs_contact_tv.setTextColor(mContext.getResources().getColor(R.color.text_gray));
//			// vs_money_tv.setTextColor(mContext.getResources().getColor(R.color.vs_gree));
//			vs_mms_tv.setTextColor(mContext.getResources().getColor(R.color.vs_gree));
//			vs_found_tv.setTextColor(mContext.getResources().getColor(R.color.text_gray));
//			vs_me_tv.setTextColor(mContext.getResources().getColor(R.color.text_gray));
//			/*
//			 * vs_service_tv.setTextColor(mContext.getResources().getColor(R.color
//			 * .text_gray));
//			 * vs_service_imagview.setBackgroundResource(R.drawable.ic_service);
//			 */
//			break;
		case 1:

			vs_contact_imagview.setBackgroundResource(R.drawable.ic_contacts_list_focused);
			// vs_money_imagview.setBackgroundResource(R.drawable.ic_recharge_normal);
			vs_found_imagview.setBackgroundResource(R.drawable.ic_service);
			vs_me_imagview.setBackgroundResource(R.drawable.ic_myself_normal);
//			vs_mms_imagview.setBackgroundResource(R.drawable.ic_mms_normal);
			vs_keyborad_tv.setTextColor(mContext.getResources().getColor(R.color.text_gray));
			vs_contact_tv.setTextColor(mContext.getResources().getColor(R.color.vs_blue_sdeep));
			// vs_money_tv.setTextColor(mContext.getResources().getColor(R.color.text_gray));
//			vs_mms_tv.setTextColor(mContext.getResources().getColor(R.color.text_gray));
			vs_found_tv.setTextColor(mContext.getResources().getColor(R.color.text_gray));
			vs_me_tv.setTextColor(mContext.getResources().getColor(R.color.text_gray));
			/*
			 * vs_service_tv.setTextColor(mContext.getResources().getColor(R.color
			 * .text_gray));
			 * vs_service_imagview.setBackgroundResource(R.drawable.ic_service);
			 */
			break;
		case 2:
//			vs_mms_imagview.setBackgroundResource(R.drawable.ic_mms_normal);
			vs_contact_imagview.setBackgroundResource(R.drawable.ic_contacts_list_normal);
			vs_found_imagview.setBackgroundResource(R.drawable.ic_service_focused);
			vs_me_imagview.setBackgroundResource(R.drawable.ic_myself_normal);
			vs_keyborad_tv.setTextColor(mContext.getResources().getColor(R.color.text_gray));
			vs_contact_tv.setTextColor(mContext.getResources().getColor(R.color.text_gray));
//			vs_mms_tv.setTextColor(mContext.getResources().getColor(R.color.text_gray));
			vs_found_tv.setTextColor(mContext.getResources().getColor(R.color.vs_blue_sdeep));
			vs_me_tv.setTextColor(mContext.getResources().getColor(R.color.text_gray));
			break;
		case 3:
			vs_contact_imagview.setBackgroundResource(R.drawable.ic_contacts_list_normal);
			// vs_money_imagview.setBackgroundResource(R.drawable.ic_recharge_normal);
			vs_found_imagview.setBackgroundResource(R.drawable.ic_service);
//			vs_mms_imagview.setBackgroundResource(R.drawable.ic_mms_normal);
			vs_keyborad_tv.setTextColor(mContext.getResources().getColor(R.color.text_gray));
			vs_contact_tv.setTextColor(mContext.getResources().getColor(R.color.text_gray));
			// vs_money_tv.setTextColor(mContext.getResources().getColor(R.color.text_gray));
//			vs_mms_tv.setTextColor(mContext.getResources().getColor(R.color.text_gray));
			vs_found_tv.setTextColor(mContext.getResources().getColor(R.color.text_gray));
			vs_me_tv.setTextColor(mContext.getResources().getColor(R.color.vs_blue_sdeep));
			vs_me_imagview.setBackgroundResource(R.drawable.ic_myself_focused);

			/*
			 * vs_service_tv.setTextColor(mContext.getResources().getColor(R.color
			 * .text_gray));
			 * vs_service_imagview.setBackgroundResource(R.drawable.ic_service);
			 */
			break;

		}
	}

	public interface OnIndicateListener {
		public void onIndicate(View v, int which);
	}

	public void setOnIndicateListener(OnIndicateListener listener) {
		mOnIndicateListener = listener;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.vs_keyborad_layout:
			mOnIndicateListener.onIndicate(v, 0);
			setIndicator(0);
			break;
		case R.id.vs_contact_layout:
			mOnIndicateListener.onIndicate(v, 1);
			setIndicator(1);
			break;
		// case R.id.vs_money_layout:
		// MobclickAgent.onEvent(mContext, "RechargeClick");
		// mOnIndicateListener.onIndicate(v, 2);
		// setIndicator(2);
		// break;
		case R.id.vs_found_layout:
			mOnIndicateListener.onIndicate(v, 2);
			setIndicator(2);
			break;
		case R.id.vs_me_layout:
			mOnIndicateListener.onIndicate(v, 3);
			setIndicator(3);
			break;
		/*
		 * case R.id.vs_service_layout: mOnIndicateListener.onIndicate(v, 4);
		 * setIndicator(4); break;
		 */
		default:
			break;
		}
	}

	/**
	 * 红点提示
	 * 
	 * @param count
	 */
	public static void showHideRed(int count) {
		if (vs_mms_unread != null) {
			if (count > 0) {
				vs_mms_unread.setVisibility(View.VISIBLE);
				vs_mms_unread.setText(count + "");
				vs_mms_imagview_red.setVisibility(View.GONE);
			} else {
				vs_mms_imagview_red.setVisibility(View.GONE);
				vs_mms_unread.setVisibility(View.INVISIBLE);
			}

		}
	}
}
